package dynamic.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * jdk动态代理，将快递员送货的这件事抽取出来实现jdk提供的接口，
 * 在程序运行是自动生成快递员这个类来执行代理
 */
public class CountTimeInvocationHandler implements InvocationHandler{

    private Transportable target;

    public CountTimeInvocationHandler(Transportable target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        long start = System.currentTimeMillis();
        method.invoke(target,args);
        long end = System.currentTimeMillis();
        System.out.println("送货耗时:"+(end - start)+"ms");
        return null;
    }
}
